package com.aplikata.evok.ui;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.primefaces.event.SelectEvent;

import com.aplikata.evok.bo.DomainPatient;
import com.aplikata.evok.bo.PatientAttachement;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.datamodel.PatientAttachementDataModel;
import com.aplikata.evok.service.EvokPatientService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;
import com.aplikata.util.StringUtil;

@ManagedBean(name = "evokPatientAttachementBean")
@ViewScoped
public class EvokPatientAttachementBean extends UIBean {

	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokPatientService}")
	private EvokPatientService patientService;

	private DomainPatient domainPatient;
	private PatientAttachement patientAtt;
	private UploadedFile uploadAtt;

	private PatientAttachementDataModel patientAttModel;

	@PostConstruct
	public void init(){
		patientAtt = new PatientAttachement();
	}
	
	public void addAction() {
		patientAtt = new PatientAttachement(domainPatient, FacesUtil.getUserFromSession());
	}

	public void resetAction(ActionEvent event) {
		addAction();
	}

	public void submitAction() {
		try {
			patientService.submitPatientAttachement(patientAtt, uploadAtt);
			uploadAtt = null;
			addAction();
			populatePatientAttachementModel();
			FacesUtil.addInfoMessage("patient_att_update_success");
			FacesUtil.unselectTableRows("wdgPatientAttList");
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
		}
	}

	public void deleteAction() {
		try {
			patientService.deletePatientAttachement(patientAtt);
			addAction();
			populatePatientAttachementModel();
			FacesUtil.addInfoMessage("patient_att_delete_success");
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
		}
	}

	@SuppressWarnings("unchecked")
	public void populatePatientAttachementModel() {
		if (domainPatient == null)
			return;
		List<PatientAttachement> attList = (List<PatientAttachement>) patientService.findByNamedQuery(
				PatientAttachement.QY_BY_PATIENT, new Object[] { domainPatient.getId() });
		if (patientAttModel == null)
			patientAttModel = new PatientAttachementDataModel(patientService, attList);
		else
			patientAttModel.setWrappedData(attList);
	}

	public void selectAtt(SelectEvent event) {
		patientAtt = (PatientAttachement) event.getObject();
	}

	public UploadedFile getUploadAtt() {
		return uploadAtt;
	}

	public void setUploadAtt(UploadedFile uploadAtt) {
		this.uploadAtt = uploadAtt;
	}

	public EvokPatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(EvokPatientService patientService) {
		this.patientService = patientService;
	}

	public DomainPatient getDomainPatient() {
		if (FacesContext.getCurrentInstance().getExternalContext().getFlash().containsKey(EvokConstants.FLASH_DOMAIN_PATIENT)) {
			domainPatient = (DomainPatient) FacesContext.getCurrentInstance().getExternalContext().getFlash()
					.get(EvokConstants.FLASH_DOMAIN_PATIENT);
			populatePatientAttachementModel();
		}
		return domainPatient;
	}

	public void setDomainPatient(DomainPatient domainPatient) {
		this.domainPatient = domainPatient;
	}

	public PatientAttachement getPatientAtt() {
		return patientAtt;
	}

	public void setPatientAtt(PatientAttachement patientAtt) {
		this.patientAtt = patientAtt;
	}

	public PatientAttachementDataModel getPatientAttModel() {
		return patientAttModel;
	}

	public void setPatientAttModel(PatientAttachementDataModel patientAttModel) {
		this.patientAttModel = patientAttModel;
	}
}
